//
//  RadioTimeView.swift
//  AirTravel
//
//  Created by apple on 2021/8/10.
//

import UIKit
import FWPopupView

protocol RadioTimeDelegate:NSObjectProtocol {
    func radioTimeClick(date:String,according:String,difference:String)
}

class RadioTimeView: FWPopupView {

    var contentView:UIView!
    weak var delegate: RadioTimeDelegate?
    let calendar = FSCalendar()
    let gregorian = NSCalendar.init(calendarIdentifier: NSCalendar.Identifier.gregorian)
    
    let dateFormatter = DateFormatter()
    @IBOutlet weak var backView: UIView!
    
    
    // 初始化时将xib中的view添加进来
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView = loadViewFromNib()
        contentView.frame = self.bounds
        contentView.backgroundColor = UIColor.white
        self.insertSubview(contentView, at: 0)
        
        self.calendar.frame = CGRect.init(x: 0, y: 0, width: ScreenWidth, height: ScreenHight * 0.8 - 61 - 72)
        self.calendar.dataSource = self
        self.calendar.delegate = self
        self.calendar.scrollDirection = .horizontal
        self.backView.addSubview(self.calendar)
        
        self.dateFormatter.dateFormat = "yyyy-MM-dd"
        let chinese = Locale.init(identifier: "zh-CN")
        self.dateFormatter.locale = chinese
    }

    // 初始化时将xib中的view添加进来
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // 加载xib
    func loadViewFromNib() -> UIView {
        let className = type(of: self)
        let bundle = Bundle(for: className)
        let name = NSStringFromClass(className).components(separatedBy: ".").last
        let nib = UINib(nibName: name!, bundle: bundle)
        let view = nib.instantiate(withOwner: self, options: nil).first as! UIView
        view.backgroundColor = UIColor.clear
        return view
    }
    
    @IBAction func backView(_ sender: UIButton) {
        self.hide()
        self.hideKeyboard()
    }
    
    func setUpData(str:String) {
        let da = self.dateFormatter.date(from: str)
        self.calendar.select(da, scrollToDate: false)
    }
    
}

extension RadioTimeView:FSCalendarDataSource,FSCalendarDelegate{
    
    func minimumDate(for calendar: FSCalendar) -> Date {
        return Date()
    }
    
    func maximumDate(for calendar: FSCalendar) -> Date {
        return (self.gregorian?.date(byAdding: NSCalendar.Unit.month, value: 10, to: Date(), options: NSCalendar.Options.matchFirst))!
    }
    
    func calendar(_ calendar: FSCalendar, titleFor date: Date) -> String? {
        if ((self.gregorian?.isDateInToday(date)) == nil) {
            return "今";
        }
        return nil;
    }
    
    func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
        let str = self.dateFormatter.string(from: date)
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM月dd日"
        let according = dateFormatter.string(from: date)
        
        dateFormatter.dateFormat = "yyyy-MM-dd"
        let jtStr = dateFormatter.string(from: Date())
        guard let date1 = dateFormatter.date(from: jtStr) else { return }
        
        let components = NSCalendar.current.dateComponents([.day], from: date1, to: date)
        
        let components1 = NSCalendar.current.dateComponents([.weekday], from: date)
        
        var difference = ""
        if components.day == 0{
            difference = "今天"
        }else if components.day == 1 {
            difference = "明天"
        }else{
            switch components1.weekday {
            case 1:
                difference = "周日"
            case 2:
                difference = "周一"
            case 3:
                difference = "周二"
            case 4:
                difference = "周三"
            case 5:
                difference = "周四"
            case 6:
                difference = "周五"
            case 7:
                difference = "周六"
            default:
                break
            }
        }
        
        self.delegate?.radioTimeClick(date: str, according: according, difference: difference)
        self.hide()
        self.hideKeyboard()
    }
}
